/**
 * @param {*} viewer 场景
 * @param {*} options 调色
 * @param {*} layerNum 图层位置
 * @param {*} restore 是否设置属性
 * @return {*}
 */
export default function modifyMap(viewer, options, layerNum = 0, restore = false) {
    const baseLayer = viewer.imageryLayers.get(layerNum)
    const { brightness, contrast, gamma, hue, saturation, invertColor, filterRGB } = options
    const baseFragShader = viewer.scene.globe._surfaceShaderSet.baseFragmentShaderSource.sources
    if (restore) {
        baseLayer.brightness = 1
        baseLayer.contrast = 1
        baseLayer.gamma = 1
        baseLayer.hue = 0
        baseLayer.saturation = 1
    } else {
        baseLayer.brightness = brightness
        baseLayer.contrast = contrast
        baseLayer.gamma = gamma
        baseLayer.hue = hue
        baseLayer.saturation = saturation
    }

    for (let i = 0; i < baseFragShader.length; i++) {
        const strS = 'color = czm_saturation(color, textureSaturation);\n#endif\n'
        let strT = 'color = czm_saturation(color, textureSaturation);\n#endif\n'
        let str = ''
        if (invertColor) {
            str += 'color.r = 1.0 - color.r;\ncolor.g = 1.0 - color.g;\ncolor.b = 1.0 - color.b;\n'
        }
        if (filterRGB.length > 0) {
            str += `color.r = color.r * ${filterRGB[0]}.0/255.0;\ncolor.g = color.g * ${filterRGB[1]}.0/255.0;\ncolor.b = color.b * ${filterRGB[2]}.0/255.0;\n`
        }
        baseFragShader[i] = baseFragShader[i].replace(str, '')
        strT += str
        baseFragShader[i] = baseFragShader[i].replace(restore ? strT : strS, restore ? strS : strT)
    }
}
